import Vue from 'vue';
import VueRouter from 'vue-router';
import PetLogin from '../views/PetLogin.vue';
import CreateProfile from '../views/CreateProfile.vue';
import ViewProfile from '../views/ViewProfile.vue';
import PetChat from '../views/PetChat.vue';
import PetDiary from '../views/PetDiary.vue';
import PetRegister from '../views/PetRegister.vue';
import HomeView from '../views/HomeView.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },{
    path: '/login',
    name: 'login',
    component: PetLogin
  },
  {
    path: '/register',
    name: 'register',
    component: PetRegister
  },
  {
    path: '/create-profile',
    name: 'create-profile',
    component: CreateProfile
  },
  {
    path: '/view-profile',
    name: 'view-profile',
    component: ViewProfile
  },
  {
    path: '/chat',
    name: 'chat',
    component: PetChat
  },
  {
    path: '/diary',
    name: 'diary',
    component: PetDiary
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  const isLoggedIn = !!localStorage.getItem('token');
  if (!isLoggedIn && to.name !== 'login' && to.name !== 'register') {
    next({ name: 'login' });
  } else {
    next();
  }
});

export default router;
